package controllers;


import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;


import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;


import model.entities.StocksMatchingInfo;
import myDaos.StocksMatchingDao;

@ManagedBean
@ViewScoped
public class StocksMatchingManagedBean {
	private List<StocksMatchingInfo> smlist;
	private String country;
	private String distributor;
	private Date to_date;
	private Date from_date;
	private boolean isProcedureCalled = false;
	private List<SelectItem> distList;

	@EJB
	private StocksMatchingDao smd;
	 
	@ManagedProperty("#{servicesManagedBean}")
	private ServicesManagedBean servicesManagedBean;

	
	
	

	public List<SelectItem> getDistList() {
		return distList;
	}


	public void setDistList(List<SelectItem> distList) {
		this.distList = distList;
	}


	public ServicesManagedBean getServicesManagedBean() {
		return servicesManagedBean;
	}


	public void setServicesManagedBean(ServicesManagedBean servicesManagedBean) {
		this.servicesManagedBean = servicesManagedBean;
	}


	public List<StocksMatchingInfo> getSmlist() {
		return smlist;
	}


	public void setSmlist(List<StocksMatchingInfo> smlist) {
		this.smlist = smlist;
	}


	public String getCountry() {
		return country;
	}


	public void setCountry(String country) {
		this.country = country;
	}


	public String getDistributor() {
		return distributor;
	}


	public void setDistributor(String distributor) {
		this.distributor = distributor;
	}


	public Date getTo_date() {
		return to_date;
	}


	public void setTo_date(Date to_date) {
		this.to_date = to_date;
	}


	public Date getFrom_date() {
		return from_date;
	}


	public void setFrom_date(Date from_date) {
		this.from_date = from_date;
	}


	public boolean isProcedureCalled() {
		return isProcedureCalled;
	}


	public void setProcedureCalled(boolean isProcedureCalled) {
		this.isProcedureCalled = isProcedureCalled;
	}


	public void callMatchingProcedure(){
		Boolean procedureReturn;
		procedureReturn=smd.callMatchingReportProcedure(country, distributor, from_date, to_date);
		if(procedureReturn)
		{
			isProcedureCalled=true;
			getMatchingList();
		}
	}

	public void getMatchingList(){
		smlist=smd.getMatchingReportList();
	}
	
	public void updateDistributorsList()
	{
		//Proposed by Samy
		distList = new ArrayList<SelectItem>();
		for(int i = 0; i < servicesManagedBean.getCountriesDistributorList().size(); i++) 
	    {  
			if(country!=null && !country.trim().equals("") && 
					  servicesManagedBean.getCountriesDistributorList().get(i).getCountry().equals(country))
			{
			distList.add(new SelectItem(
										 servicesManagedBean.getCountriesDistributorList().get(i).getDistCode()
										 ,
										 servicesManagedBean.getCountriesDistributorList().get(i).getDistCode()
					                    )
			             );
			}
	    }
	}
	
	@PostConstruct
	public void init(){
		to_date = new Date();
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTime(to_date);
		gc.add(Calendar.DAY_OF_YEAR, -1);
		from_date= gc.getTime();
	}
}
